var oTxt = document.getElementById('z1');
var oSpan = document.getElementById('sp');
var oPass = document.getElementById('z2');
var oDenglu = document.getElementById('rig');

oTxt.onblur = function() {
	var xhr = new XMLHttpRequest();
	xhr.open('post', '../php/denglu.php');
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.send('username=' + oTxt.value + '&password=' + oPass.value);
	xhr.onreadystatechange = function() {
		// 响应完成并且响应成功
		if (xhr.readyState == 4 && xhr.status == 200) {
			if (xhr.responseText == '0') {
				oSpan.innerHTML = '可以使用';
				oSpan.style.color = 'green';
			} else {
				oSpan.innerHTML = '太受欢迎了，请重新输入吧';
				oSpan.style.color = 'red';
			}
		}
	}
}
oDenglu.onclick = function() {
	var xhr = new XMLHttpRequest();
	xhr.open('post', '../php/denglu.php');
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.send('username=' + oTxt.value + '&password=' + oPass.value);
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {
			if (xhr.responseText == '0') {
				alert('登录失败,请前往注册');
			} else {
				saveCookie("username",$("#username").val());
				location.href = '../html/shouye.html';
			}
		}
	}
}
